package org.qloudgen.borg.result;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.Cluster;

public class HiveReader implements IDBReader{
	
	Connection con ;
	Statement stmt ;
	ResultSet res = null;
	String url,user,password;
	
	public HiveReader(Cluster cluster) throws Exception{
		
		
		Class.forName( (String) CommonValues.props.get("driver")).newInstance();
        url =  "jdbc:hive://"+ cluster.getMasterIp() +":10000/default";
        user = (String)CommonValues.props.get("user");
        password = (String)CommonValues.props.get("password") ;
	}
	
	public ResultSet query(String sql){
		
		try {
			
			con = DriverManager.getConnection(url, user , password);
			stmt = con.createStatement();
			res = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return	res;		
	}
	
    public void close(){
    	try {
    		if (res != null) res.close();
    		if (stmt != null) stmt.close();
    		if (con != null) con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    	
}
